package com.springcloud.generate.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

public class PropertiesUtil {

    private static final Logger logger = LogManager.getLogger(PropertiesUtil.class);

    public static String getProperties(String key) {
        Properties properties = new Properties();
        FileInputStream fileInputStream = null;

        try {
            ClassLoader classLoader = PropertiesUtil.class.getClassLoader();

            URL url = classLoader.getResource("application.properties");

            String path = url.getPath();
            fileInputStream = new FileInputStream(path);
            properties.load(fileInputStream);

            String propertyValue = properties.getProperty(key);
            logger.info("propertyValue: " + propertyValue);

            return propertyValue;
        } catch (Exception e) {
            e.printStackTrace();

            logger.error("配置读取失败!");
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return "";
    }
}
